import { Connection } from 'mongoose';
import { getInstance } from '../../db';
import { videoSchema } from '../../schemas/videoSchema';
import { countTotal, limitPage } from '../../../util/dbOpt';

export const getVideoList = async (params: IAnyObject): Promise<IAnyObject> => {
  const db: Connection = await getInstance();
  const videoModel = db.model('video', videoSchema);

  const { keyword, pageSize, pageNum } = params;

  const queryCondition = keyword ? {
    $or: [{ metric_name: { $regex: keyword } }]
  } :
  {};

  // 排序，暂不需要
  const sortCondition = {
    // _id: 1,
  }

  // 总数
  const total = await countTotal(videoModel ,queryCondition);

  // 分页
  const dataList = await limitPage(
    videoModel,
    pageSize,
    pageNum,
    queryCondition,
    sortCondition
  );

  const result = {
    list: dataList,
    pageSize,
    pageNum,
    total,
  }

  return result;
}